#include "TexturePng.h"
#include <iostream>
#include <gl\GL.h>

#include <libpng\png.h>
#include <libpng\pngconf.h>
#include <libpng\zconf.h>
#include <libpng\zlib.h>

#pragma warning(disable : 4611)

TexturePng::TexturePng():
mWidth(0), mHeight(0),
mDepth(0), mColorType(0), mInterlaceType(0),
mData(0)
{
}

TexturePng::~TexturePng()
{
}

void PngReadProc(png_structp png_ptr, png_bytep data, png_size_t length)
{
	ReadFile(png_get_io_ptr(png_ptr), data, length, (DWORD*)&length, NULL);
}

void to4bpp(png_structp png_ptr, png_row_infop row_info, png_bytep data)
{
	png_ptr;

	static const png_byte pix[] = {
		0x00, 0x01, 0x02, 0x03, 0x10, 0x11, 0x12, 0x13,
		0x20, 0x21, 0x22, 0x23, 0x30, 0x31, 0x32, 0x33,
	};

	png_uint_32 rowb;
	png_byte *p, *q, c;

	rowb = (row_info->width + 1) / 2;
	q = data + rowb;
	p = data + rowb / 2;

	if (rowb % 2 == 1){
		c = *p;
		*(--q) = pix[c >> 4];
	}

	while(p > data) {
		c = *(--p);
		*(--q) = pix[c & 0x0f];
		*(--q) = pix[c >> 4];
	}

	row_info->bit_depth = 4;
	row_info->pixel_depth = 4;
	row_info->rowbytes = rowb;
}

bool TexturePng::Load(const char* pFileName)
{
	//header for testing if it is a png
	png_byte header[8];

	//open file as binary
	FILE *fp = fopen(pFileName, "rb");
	if (!fp) {
		return false;
	}

	//read the header
	fread(header, 1, 8, fp);

	//test if png
	int is_png = !png_sig_cmp(header, 0, 8);
	if (!is_png) {
		fclose(fp);
		return false;
	}

	//create png struct
	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
		NULL, NULL);
	if (!png_ptr) {
		fclose(fp);
		return (false);
	}

	//create png info struct
	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
		fclose(fp);
		return (false);
	}

	//create png info struct
	png_infop end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
		fclose(fp);
		return (false);
	}

	//png error stuff, not sure libpng man suggests this.
	if (setjmp(png_jmpbuf(png_ptr))) {
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(fp);
		return (false);
	}

	//init png reading
	png_init_io(png_ptr, fp);

	//let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	//variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 twidth, theight;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
		NULL, NULL, NULL);

	GLuint pixelFormat = 0;
	if (color_type == PNG_COLOR_TYPE_RGB)
		pixelFormat = GL_RGB;
	else if (color_type == PNG_COLOR_TYPE_RGBA)
		pixelFormat = GL_RGBA;

	//update width and height based on png info
	mWidth = twidth;
	mHeight = theight;

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// Allocate the image_data as a big block, to be given to opengl
	png_byte *image_data = new png_byte[(mWidth * sizeof(color_type)) * mHeight];
	if (!image_data) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(fp);
		return false;
	}

	//row_pointers is for pointing to image_data for reading the png with libpng
	png_bytep *row_pointers = new png_bytep[mHeight];
	if (!row_pointers) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		delete[] image_data;
		fclose(fp);
		return false;
	}
	// set the individual row_pointers to point at the correct offsets of image_data
	for (unsigned int i = 0; i < mHeight; ++i)
		row_pointers[mHeight - 1 - i] = image_data + i * rowbytes;

	//read the png into image_data through row_pointers
	png_read_image(png_ptr, row_pointers);

	//Now generate the OpenGL texture object
	glGenTextures(1, &mTextureName);
	glBindTexture(GL_TEXTURE_2D, mTextureName);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0,
		pixelFormat, GL_UNSIGNED_BYTE, (GLvoid*)image_data);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	//clean up memory and close stuff
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	delete[] image_data;
	delete[] row_pointers;
	fclose(fp);

	return true;
}

void TexturePng::Release()
{
	free(mData);
}